from django.template import loader,Context
from django.http import HttpResponse,Http404
from django.views.generic.list_detail import object_list
from models import *
from UserProfile import *
from django.contrib.auth.models import User,UserManager
from django.db import models
from django.db.models import *
from django.contrib import admin,auth
from decorator import *
from django.contrib.auth.decorators import *

def login(request):
    if request.method == 'GET':
        #s = Student.objects.filter(username=request.username)
        t = loader.get_template("login.html")
        c = Context()
        return HttpResponse(t.render(c))
    else: 
        post = request.POST.copy()
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        user = auth.authenticate(username=username, password=password)
        
        if user is not None:
            auth.login(request, user)
        else:
            print "wrong user"
            raise Http404
        raise Http404

def register(request):
    if request.method == 'GET':
        t = loader.get_template('register.html')
        c = Context()
        return HttpResponse(t.render(c))
    else:
        post = request.POST.copy()
        usrname = post["username"]
        passwrd = post["password"]
        first = post["first_name"]
        last = post["last_name"]
        email = post["email"]
        intro = post["selfIntro"]

        user = User.objects.create_user(usrname,email,passwrd)
        profile = UserProfile.objects.create(user)
        user.is_staff = True;
        profile.selfIntro = intro
        user.firstname = first
        user.lastname = last
        user.save()
        raise Http404
    
@login_required
@user_passes_test(user_is_teacher,login_url="../NotTeacher")
def AssignHomework(request):
    if request.method == 'GET':
        t = loader.get_template('AssignHomework.html')
        c = Context()
        return HttpResponse(t.render(c))
    else:
        post = request.POST.copy()
        title = post['title']
        content = post['content']
        now = datetime.datetime.now()
        
        u = request.user
        print u
        
        homework = Homework.objects.create()
        homework.title = title
        homework.content = title
        homework.deadline = now
        homework.publishTime = now
        homework.type=1
        homework.save()
        
        for s in User.objects.all():
            if s.get_profile().type ==2:
                StudentHomework.objects.create(s,homework)
            
        raise Http404
        
@login_required
@user_passes_test(user_can_createGroup,login_url="../Already_In_A_Group")
def createGroup(request):
    if request.method == 'GET':
        t = loader.get_template('createGroup.html')
        c = Context()
        return HttpResponse(t.render(c))
    else:
         post = request.POST.copy()
         n = post["name"]
         i = post["intro"]
         user = request.user
         group = Group.objects.create(name = n, introduction = i,leader = user)
         p = user.get_profile()
         p.group = group
         p.save()
         raise Http404
         
         
@login_required
@user_passes_test(user_in_group,login_url="../DoNotHaveAGroup")
def quitGroup(request):
    if request.method == 'GET':
        t = loader.get_template('quitGroup.html')
        c = Context()
        return HttpResponse(t.render(c))
    else:        
         user = request.user
         pro = user.get_profile()
         
         group = pro.group
         group.quit(user)
    
         pro.group = Group.objects.filter(name="DefaultGroup")[0]
         pro.save()
         raise Http404
        
        